package br.com.cnpv.modelo.dao;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import br.com.cnpv.modelo.acesso.idao.IEventoDAO;
import br.com.cnpv.modelo.entity.Evento;
import br.com.cnpv.modelo.entity.Usuario;

public class EventoDAO implements IEventoDAO{

	@Override
	public Evento saveOrUpdate(Evento evento, Session session)
			throws HibernateException {
		return (Evento) session.merge(evento);
	}

	@Override
	public int obtemTotal(Usuario usuario, int ano, Session session)
			throws HibernateException {
		
		Query query = session.getNamedQuery("Evento.obtemTotal");
		
		query.setString("dtEvento", "%"+ano+"%");
		query.setInteger("idAssociacao", usuario.getIdAssociacao());
		
		int retorno = query.list().size();
		
		if(retorno == 0)
 			retorno = 1;
		
		return retorno;
	}
}